package com.qumitech.visualnovel.service.associate.impl;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.util.CommonUtil;
import com.qumitech.visualnovel.datasource.console.AccountLoginMapper;
import com.qumitech.visualnovel.datasource.console.AccountRegisterMapper;
import com.qumitech.visualnovel.datasource.console.OrderDOMapper;
import com.qumitech.visualnovel.pubdef.dto.associate.AssociateAccountDTO;
import com.qumitech.visualnovel.service.associate.AssociateAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class AssociateAccountServiceImpl implements AssociateAccountService {

    @Autowired
    AccountRegisterMapper accountRegisterMapper;

    @Autowired
    AccountLoginMapper accountLoginMapper;


    @Autowired
    OrderDOMapper orderDOMapper;

    @Override
    public JSONObject registerRank(String startTime, String endTime) {
        List<HashMap <String, String>> deviceIdRank = accountRegisterMapper.getRank("device_id", startTime, endTime);
        List<HashMap <String, String>> clientIpRank = accountRegisterMapper.getRank("client_ip", startTime, endTime);
        List<HashMap <String, String>> deviceMacRank = accountRegisterMapper.getRank("device_mac", startTime, endTime);
        List<HashMap <String, String>> deviceImeiRank = accountRegisterMapper.getRank("device_imei", startTime, endTime);
        List<HashMap <String, String>> locationRank = accountRegisterMapper.getRank("location", startTime, endTime);
        JSONObject res = new JSONObject();
        res.put("register_device_id", deviceIdRank);
        res.put("register_client_ip", clientIpRank);
        res.put("register_device_mac", deviceMacRank);
        res.put("register_device_imei", deviceImeiRank);
        res.put("register_location", locationRank);

        return res;
    }

    @Override
    public JSONObject loginRank(String startTime, String endTime) {
        List<String> tableNameList = CommonUtil.getTableListFromDate(startTime,endTime, "account_login");
        List<HashMap <String, String>> deviceIdRank = accountLoginMapper.getRank(tableNameList,"device_id", startTime, endTime);
        List<HashMap <String, String>> clientIpRank = accountLoginMapper.getRank(tableNameList,"client_ip", startTime, endTime);
        List<HashMap <String, String>> deviceMacRank = accountLoginMapper.getRank(tableNameList,"device_mac", startTime, endTime);
        List<HashMap <String, String>> deviceImeiRank = accountLoginMapper.getRank(tableNameList,"device_imei", startTime, endTime);
        List<HashMap <String, String>> locationRank = accountLoginMapper.getRank(tableNameList,"location", startTime, endTime);
        JSONObject res = new JSONObject();
        res.put("login_device_id", deviceIdRank);
        res.put("login_client_ip", clientIpRank);
        res.put("login_device_mac", deviceMacRank);
        res.put("login_device_imei", deviceImeiRank);
        res.put("login_location", locationRank);
        return res;
    }

    @Override
    public JSONObject payRank(String startTime, String endTime) {
        List<HashMap <String, String>> payIdRank = orderDOMapper.getRank(startTime,endTime);
        JSONObject res = new JSONObject();
        res.put("payer_id", payIdRank);
        return res;
    }

    @Override
    public List<AssociateAccountDTO> listAccountByDimension(String dimension,String dimensionValue, String startTime, String endTime, String orderType, String orderBy, Integer offset, Integer limit) {

        if (dimension.contains("register")){
            dimension = dimension.replace("register_", "");
           return accountRegisterMapper.listAccountByDimension(dimension,dimensionValue,startTime,endTime,orderType,orderBy,offset,limit);
        }
        else if (dimension.contains("login")){
            dimension = dimension.replace("login_", "");
            List<String> tableNameList = CommonUtil.getTableListFromDate(startTime,endTime, "account_login");
            return accountLoginMapper.listAccountByDimension(tableNameList, dimension,dimensionValue,startTime,endTime,orderType,orderBy,offset,limit);
        }
        else {
            dimension = "payer_id";
            return orderDOMapper.listAccountByDimension(dimension, dimensionValue,startTime,endTime,orderType,orderBy,offset,limit);

        }
    }

    @Override
    public int countAccountByDimension(String dimension, String dimensionValue, String startTime, String endTime) {
        if (dimension.contains("register")){
            dimension = dimension.replace("register_", "");
            return accountRegisterMapper.countAccountByDimension(dimension,dimensionValue,startTime,endTime);
        }
        else if (dimension.contains("login")){
            dimension = dimension.replace("login_", "");
            List<String> tableNameList = CommonUtil.getTableListFromDate(startTime,endTime, "account_login");
            return accountLoginMapper.countAccountByDimension(tableNameList,dimension, dimensionValue, startTime,endTime);
        }
        else{
            dimension = "payer_id";
            return orderDOMapper.countAccountByDimension(dimension, dimensionValue,startTime,endTime);
        }
    }

    @Override
    public List<HashMap<String, String>> listRankByDimension(String dimension, String startTime, String endTime, String orderType, String orderBy, Integer offset, Integer limit) {
        if (dimension.contains("register")){
            dimension = dimension.replace("register_", "");
            return accountRegisterMapper.listRankByDimension(dimension,startTime,endTime,orderType,orderBy,offset,limit);
        }
        else if (dimension.contains("login")){
            dimension = dimension.replace("login_", "");
            List<String> tableNameList = CommonUtil.getTableListFromDate(startTime,endTime, "account_login");
            return accountLoginMapper.listRankByDimension(tableNameList, dimension,startTime,endTime,orderType,orderBy,offset,limit);
        }
        else {
            dimension = "payer_id";
            return orderDOMapper.listRankByDimension(dimension,startTime,endTime,orderType,orderBy,offset,limit);

        }
    }

    @Override
    public int countRankByDimension(String dimension, String startTime, String endTime) {
        if (dimension.contains("register")){
            dimension = dimension.replace("register_", "");
            return accountRegisterMapper.countRankByDimension(dimension,startTime,endTime);
        }
        else if (dimension.contains("login")){
            dimension = dimension.replace("login_", "");
            List<String> tableNameList = CommonUtil.getTableListFromDate(startTime,endTime, "account_login");
            return accountLoginMapper.countRankByDimension(tableNameList,dimension, startTime,endTime);
        }
        else{
            dimension = "payer_id";
            return orderDOMapper.countRankByDimension(dimension,startTime,endTime);
        }
    }
}
